package _interview150;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 150. 逆波兰表达式求值
 */
public class No150 {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for (String token : tokens) {
            stack.push(switch (token) {
                case "+" -> stack.pop() + stack.pop();
                case "-" -> {
                    Integer suf = stack.pop();
                    yield stack.pop() - suf;
                }
                case "*" -> stack.pop() * stack.pop();
                case "/" -> {
                    Integer suf = stack.pop();
                    yield stack.pop() / suf;
                }
                default -> Integer.parseInt(token);
            });
        }
        return stack.peek();
    }
}
